
function init() {
  var _this = this
  var moon = new Image();
  moon.src = require('../../images/redbag.png')
  moon.onload = function () {

    var context;
    var arr = new Array();
    var starCount = 800;

    var rains = new Array();
    var rainCount = 30;


    //获取canvas
    var stars = document.getElementById("stars");
    var windowWidth = window.innerWidth; //当前的窗口的高度
    stars.width = windowWidth;
    stars.height = window.innerHeight;
    //获取context
    var context = stars.getContext("2d");

    /*红包雨开始*/
    var MeteorRain = function () {
      this.x = -1;
      this.y = -1;
      this.length = -1;//长度
      this.angle = 90; //倾斜角度
      this.width = -1;//宽度
      this.height = -1;//高度
      this.speed = 1;//速度
      this.offset_x = 0;//横轴移动偏移量
      this.offset_y = 0;//纵轴移动偏移量
      this.alpha = 1; //透明度
      this.color1 = "";//红包的色彩
      this.color2 = "";  //红包的色彩
      /****************初始化函数********************/
      this.init = function () { //初始化
        this.getPos();
        this.alpha = 1;//透明度
        // this.getRandomColor();
        //最小长度，最大长度
        var x = Math.random() * 80 + 150;
        this.length = Math.ceil(x);
        this.angle = Math.random() * 50 + 75; //红包倾斜角
        x = Math.random() + .4;
        this.speed = x; //红包的速度
        var cos = Math.cos(this.angle * 3.14 / 180);
        var sin = Math.sin(this.angle * 3.14 / 180) * 5;
        this.width = this.length * cos;  //红包所占宽度
        this.height = this.length * sin;//红包所占高度
        this.offset_x = this.speed * cos;
        this.offset_y = this.speed * sin;
      }

      /***************重新计算红包坐标的函数******************/
      this.countPos = function () //
      {
        //往左下移动,x减少，y增加
        this.x = this.x - this.offset_x;
        this.y = this.y + this.offset_y;
      }

      /*****************获取随机坐标的函数*****************/
      this.getPos = function () //
      {
        //横坐标200--1200

        this.x = Math.random() * window.innerWidth; //窗口高度
        //纵坐标小于600
        this.y = Math.random() * window.innerHeight;  //窗口宽度
      }
      /****绘制红包***************************/
      this.draw = function () //绘制一个红包的函数
      {

        context.drawImage(moon, this.x + this.width, this.y - this.height, 59, 77);

        // context.save();
        // context.beginPath();
      }
      this.move = function () {
        //清空红包像素
        var x = this.x + this.width - this.offset_x;
        var y = this.y - this.height;
        //重新计算位置，往左下移动
        this.countPos();
        //透明度增加
        this.alpha -= 0.002;
        //重绘
        this.draw();
      }

    }

    //绘制红包
    function playRains() {
      // context.clearRect(0,0, $("#stars").width(), ;
      stars.height = stars.height
      for (var n = 0; n < rainCount; n++) {
        var rain = rains[n];
        rain.move();//移动
        if (rain.y > window.innerHeight * 3) {//超出界限后重来
          rains[n] = new MeteorRain();
          rains[n].init();
        }
      }
      if (_this.canvasShow == true) {
        var redBagTimer = setTimeout(function () {
          playRains()
        }, 2);
      } else {
        $("#stars").fadeOut(0);
        clearTimeout(redBagTimer)
      }
    }


    //画红包
    for (var i = 0; i < rainCount; i++) {
      var rain = new MeteorRain();
      rain.init();
      rain.draw();
      rains.push(rain);
    }
    playRains();//绘制红包
  }
}